
package com.android.samples.mediastore.hms.drive.utils

import java.io.File
import java.util.*

object MimeType {
    // Huawei Drive Folder Type
    const val FOLDER = "application/vnd.huawei-apps.folder"

    // Default File Type
    const val DEFAULT = "*/*"

    // All cached MimeType types based on file suffixes
    val MIME_TYPE_MAP: MutableMap<String, String> = HashMap()

    /**
     * Match the MIMETYPE based on the file name suffix.
     *
     * @param suffix File Name suffix
     * @return MimeType
     */
    fun mimeType(suffix: String): String {
        return if (MIME_TYPE_MAP.keys.contains(suffix)) {
            MIME_TYPE_MAP[suffix]!!
        } else {
            DEFAULT
        }
    }

    /**
     * Match the MimeType based on the file type
     *
     * @param file java.io.File object
     * @return MimeType
     */
    fun mimeType(file: File): String {
        if (!file.exists()) {
            return DEFAULT
        }
        val fileName = file.name
        val suffix = fileName.substring(fileName.lastIndexOf("."))
        return mimeType(
            suffix
        )
    }

    init {
        MIME_TYPE_MAP[".3gp"] = "video/3gpp"
        MIME_TYPE_MAP[".apk"] =
            "application/vnd.android.package-archive"
        MIME_TYPE_MAP[".asf"] = "video/x-ms-asf"
        MIME_TYPE_MAP[".avi"] = "video/x-msvideo"
        MIME_TYPE_MAP[".bin"] = "application/octet-stream"
        MIME_TYPE_MAP[".bmp"] = "image/bmp"
        MIME_TYPE_MAP[".c"] = "text/plain"
        MIME_TYPE_MAP[".class"] = "application/octet-stream"
        MIME_TYPE_MAP[".conf"] = "text/plain"
        MIME_TYPE_MAP[".cpp"] = "text/plain"
        MIME_TYPE_MAP[".doc"] = "application/msword"
        MIME_TYPE_MAP[".docx"] =
            "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
        MIME_TYPE_MAP[".xls"] = "application/vnd.ms-excel"
        MIME_TYPE_MAP[".xlsx"] =
            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
        MIME_TYPE_MAP[".exe"] = "application/octet-stream"
        MIME_TYPE_MAP[".gif"] = "image/gif"
        MIME_TYPE_MAP[".gtar"] = "application/x-gtar"
        MIME_TYPE_MAP[".gz"] = "application/x-gzip"
        MIME_TYPE_MAP[".h"] = "text/plain"
        MIME_TYPE_MAP[".htm"] = "text/html"
        MIME_TYPE_MAP[".html"] = "text/html"
        MIME_TYPE_MAP[".jar"] = "application/java-archive"
        MIME_TYPE_MAP[".java"] = "text/plain"
        MIME_TYPE_MAP[".jpeg"] = "image/jpeg"
        MIME_TYPE_MAP[".jpg"] = "image/jpeg"
        MIME_TYPE_MAP[".js"] = "application/x-javascript"
        MIME_TYPE_MAP[".log"] = "text/plain"
        MIME_TYPE_MAP[".m3u"] = "audio/x-mpegurl"
        MIME_TYPE_MAP[".m4a"] = "audio/mp4a-latm"
        MIME_TYPE_MAP[".m4b"] = "audio/mp4a-latm"
        MIME_TYPE_MAP[".m4p"] = "audio/mp4a-latm"
        MIME_TYPE_MAP[".m4u"] = "video/vnd.mpegurl"
        MIME_TYPE_MAP[".m4v"] = "video/x-m4v"
        MIME_TYPE_MAP[".mov"] = "video/quicktime"
        MIME_TYPE_MAP[".mp2"] = "audio/x-mpeg"
        MIME_TYPE_MAP[".mp3"] = "audio/x-mpeg"
        MIME_TYPE_MAP[".mp4"] = "video/mp4"
        MIME_TYPE_MAP[".mpc"] = "application/vnd.mpohun.certificate"
        MIME_TYPE_MAP[".mpe"] = "video/mpeg"
        MIME_TYPE_MAP[".mpeg"] = "video/mpeg"
        MIME_TYPE_MAP[".mpg"] = "video/mpeg"
        MIME_TYPE_MAP[".mpg4"] = "video/mp4"
        MIME_TYPE_MAP[".mpga"] = "audio/mpeg"
        MIME_TYPE_MAP[".msg"] = "application/vnd.ms-outlook"
        MIME_TYPE_MAP[".ogg"] = "audio/ogg"
        MIME_TYPE_MAP[".pdf"] = "application/pdf"
        MIME_TYPE_MAP[".png"] = "image/png"
        MIME_TYPE_MAP[".pps"] = "application/vnd.ms-powerpoint"
        MIME_TYPE_MAP[".ppt"] = "application/vnd.ms-powerpoint"
        MIME_TYPE_MAP[".pptx"] =
            "application/vnd.openxmlformats-officedocument.presentationml.presentation"
        MIME_TYPE_MAP[".prop"] = "text/plain"
        MIME_TYPE_MAP[".rc"] = "text/plain"
        MIME_TYPE_MAP[".rmvb"] = "audio/x-pn-realaudio"
        MIME_TYPE_MAP[".rtf"] = "application/rtf"
        MIME_TYPE_MAP[".sh"] = "text/plain"
        MIME_TYPE_MAP[".tar"] = "application/x-tar"
        MIME_TYPE_MAP[".tgz"] = "application/x-compressed"
        MIME_TYPE_MAP[".txt"] = "text/plain"
        MIME_TYPE_MAP[".wav"] = "audio/x-wav"
        MIME_TYPE_MAP[".wma"] = "audio/x-ms-wma"
        MIME_TYPE_MAP[".wmv"] = "audio/x-ms-wmv"
        MIME_TYPE_MAP[".wps"] = "application/vnd.ms-works"
        MIME_TYPE_MAP[".xml"] = "text/plain"
        MIME_TYPE_MAP[".z"] = "application/x-compress"
        MIME_TYPE_MAP[".zip"] = "application/x-zip-compressed"
    }
}